package com.efeichong.dynamic;

import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * @author lxk
 * @date 2022/8/28
 * @description 只有在配置spring.datasource.dynamic时才加载多数据源配置
 */
public class DynamicDataSourceCondition implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = conditionContext.getEnvironment();
        BindResult<Map> bind = Binder.get(environment).bind("spring.datasource.dynamic", Map.class);
        return bind.isBound();
    }

}